using System.Windows.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Keyboard = Microsoft.Xna.Framework.Input.Keyboard;

namespace Silverlight3dApp.Learning.Lesson06
{
    public class MadnessGame : Game
    {
        private Camera _camera;

        private VertexPositionTexture[] _vertexPositionTextures;

        private VertexBuffer _vertexBuffer;
        private BasicEffect _basicEffect;

        private Texture2D _texture;

        private Matrix _worldTranslation = Matrix.Identity;
        private Matrix _worldRotation = Matrix.Identity;
        protected override void Initialize()
        {
            _camera = new Camera(this, new Vector3(0, 0, 5), new Vector3(0, 0, 0), Vector3.Up);
            Components.Add(_camera);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            _vertexPositionTextures = new VertexPositionTexture[4];
            _vertexPositionTextures[0] = new VertexPositionTexture(new Vector3(-1, 1, 0), Vector2.Zero);
            _vertexPositionTextures[1] = new VertexPositionTexture(new Vector3(1, 1, 0), Vector2.UnitX);
            _vertexPositionTextures[2] = new VertexPositionTexture(new Vector3(-1, -1, 0), Vector2.UnitY);
            _vertexPositionTextures[3] = new VertexPositionTexture(new Vector3(1, -1, 0), Vector2.One);

            _vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, _vertexPositionTextures.Length, BufferUsage.None);
            _vertexBuffer.SetData(_vertexPositionTextures);

            _basicEffect = new BasicEffect(GraphicsDevice);

            _texture = Content.Load<Texture2D>(@"Textures/Trees");

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            var keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Key.Left))
            {
                _worldTranslation *= Matrix.CreateTranslation(-0.01f, 0, 0);
            }
            if (keyState.IsKeyDown(Key.Right))
            {
                _worldTranslation *= Matrix.CreateTranslation(0.01f, 0, 0);
            }
            _worldRotation *= Matrix.CreateRotationY(MathHelper.PiOver4 / 60);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            if (GraphicsDevice.RasterizerState.CullMode != CullMode.None)
            {
                GraphicsDevice.RasterizerState = new RasterizerState
                                                     {
                                                         CullMode = CullMode.None
                                                     };
                GraphicsDevice.SamplerStates[0] = new SamplerState
                                                      {
                                                          AddressU = TextureAddressMode.Clamp,
                                                          AddressV = TextureAddressMode.Clamp
                                                      };
            }
            GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Colors.CornflowerBlue, 1.0f, 0);

            _basicEffect.World = _worldRotation * _worldTranslation;
            _basicEffect.View = _camera.View;
            _basicEffect.Projection = _camera.Projection;
            //_basicEffect.VertexColorEnabled = true;
            _basicEffect.Texture = _texture;
            _basicEffect.TextureEnabled = true;

            foreach (var pass in _basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.SetVertexBuffer(_vertexBuffer);
                GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
            }

            base.Draw(gameTime);
        }
    }
}